package hhh.babysleep.presenter.module;

import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import hhh.babysleep.R;
import hhh.babysleep.model.bean.User;
import hhh.babysleep.presenter.MyApp;
import hhh.babysleep.presenter.base.BaseActivity;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseActivity {

    @BindView(R.id.dbinsert)
    Button dbinsert;
    @BindView(R.id.dbread)
    Button dbread;
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.pass)
    EditText pass;

    private List<User> users;
    private BaseAdapter adapter;


    @Override
    protected void initData() {
        users=new ArrayList<User>();
        adapter=new CommonAdapter<User>(this,R.layout.item_teacher,users) {
            @Override
            protected void convert(ViewHolder viewHolder, User item, int position) {
                viewHolder.setText(R.id.name,item.getName());
                viewHolder.setText(R.id.pass,item.getPass());
            }
        };
        list.setAdapter(adapter);
        loadUsers();
    }

    @Override
    protected int getFragmentContentId() {
        return R.layout.activity_main;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }


    @OnClick({R.id.dbinsert, R.id.dbread})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dbinsert:
                Observable.create(new ObservableOnSubscribe<List<User>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<User>> e) throws Exception {
                        MyApp.getDaoSession().getUserDao().insert(new User((long)0,name.getText().toString(),pass.getText().toString()));
                    }
                }).subscribeOn(Schedulers.io());
                break;
            case R.id.dbread:
                loadUsers();
                break;
        }
    }

    public void loadUsers(){
        Observable.create(new ObservableOnSubscribe<List<User>>() {
            @Override
            public void subscribe(ObservableEmitter<List<User>> e) throws Exception {
                e.onNext(MyApp.getDaoSession().getUserDao().loadAll());
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<User>>() {
                    @Override
                    public void accept(List<User> u) throws Exception {
                        users.clear();
                        users.addAll(u);
                        adapter.notifyDataSetChanged();
                    }
                });

    }

}